<template>
  <div>
    <el-row style="margin-top: 50px">
      <el-form :model="form" :rules="rules" ref="saveForm" label-width="100px">
        <el-row style="margin-left: 180px">
          <el-avatar :size="150" :src="(form.icon != null && form.icon != '') ? form.icon : 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'"></el-avatar>
        </el-row>
        <el-row style="margin-top: 15px; margin-left: 100px; text-align: left" v-show="!isPassword">
          <span style="font-size: 20px">ID：</span>
          <el-input v-model="form.id" style="width: 250px; font-size: 20px" disabled></el-input>
        </el-row>
        <el-row style="margin-top: 15px; margin-left: 100px; text-align: left" v-show="!isPassword">
          <span style="font-size: 20px">用户名：</span>
          <el-input v-model="form.username" style="width: 200px; font-size: 20px" disabled></el-input>
        </el-row>
        <el-row style="margin-top: 15px; margin-left: 100px; text-align: left" v-show="!isPassword">
          <span style="font-size: 20px">姓名：</span>
          <el-input v-model="form.nickname" style="width: 200px; font-size: 20px" disabled></el-input>
        </el-row>
        <el-row style="margin-top: 15px; margin-left: 100px; text-align: left" v-show="!isPassword">
          <span style="font-size: 20px">角色：</span>
          <el-input v-model="form.roleName" style="width: 200px; font-size: 20px" disabled></el-input>
        </el-row>
        <el-row style="margin-top: 15px; margin-left: 200px;text-align: left" v-show="!isPassword">
          <el-button type="info" @click="handlePassword">修改密码</el-button>
        </el-row>
      </el-form>
      <el-form :model="passwordForm" :rules="rules" ref="passwordForm" label-width="100px">
        <el-row style="margin-top: 20px;text-align: left" v-show="isPassword">
          <el-form-item prop="oldPassword">
            <span style="font-size: 20px">原来的密码：</span>
            <div style="display: inline-block">
              <el-input v-model="passwordForm.oldPassword" style="width: 250px; font-size: 20px" clearable show-password></el-input>
            </div>
          </el-form-item>
          <el-form-item prop="newPassword">
            <span style="font-size: 20px">新密码：</span>
            <div style="display: inline-block">
              <el-input v-model="passwordForm.newPassword" style="width: 250px; font-size: 20px" clearable show-password></el-input>
            </div>
          </el-form-item>
          <el-form-item prop="newPassword2">
            <span style="font-size: 20px">确认密码：</span>
            <div style="display: inline-block">
              <el-input v-model="passwordForm.newPassword2" style="width: 250px; font-size: 20px" clearable show-password></el-input>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="margin-left: 110px" @click="savePassword">修改</el-button>
            <el-button type="info" @click="handlePassword">返回</el-button>
          </el-form-item>
        </el-row>

      </el-form>
    </el-row>
  </div>
</template>

<script>
export default {
  name: "AdminUserInfo",
  data() {
    //自定义规则
    let validatePass2 = (rule, value, callback) => {
      if (value !== this.passwordForm.newPassword) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      form: {},
      updateIconUrl: this.$store.state.uploadUrl + "/admin/file/upload/picture",
      //是否开启修改密码
      isPassword: false,
      //密码表单
      passwordForm: {},
      rules: {
        oldPassword: [
          { required: true, message: '请输入原来的密码', trigger: 'blur' },
          { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }
        ],
        newPassword: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }
        ],
        newPassword2: [
          { required: true, message: '请输入确认密码', trigger: 'blur' },
          { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' },
          {validator: validatePass2, trigger: 'blur'}
        ],
      }
    };
  },
  created() {
    this.loadUserData();
  },
  methods: {
    loadUserData() {
      this.request.get("/admin/user/me").then(res => {
        if (res.code == 200) {
          this.form = res.data;
          if (res.data.roleId == "2") {
            this.form.roleName = "管理员"
          } else if (res.data.roleId == "1") {
            this.form.roleName = "经理"
          }
        }
      });
    },
    handlePassword() {
      this.isPassword = !this.isPassword;
      this.passwordForm = {};
    },
    //修改密码
    savePassword() {
      // 表单校验函数
      this.$refs['passwordForm'].validate((valid) => {
        if (valid) {  //ui验证是否通过
          this.request.post("/admin/user/password", this.passwordForm).then(res => {
            if(res.code == 200) {
              this.$message.success(res.msg);
              localStorage.removeItem("token");
              this.$router.push("/login");
            }else {
              this.$message.error(res.msg);
            }
          })
        } else {
          return false;
        }
      });
    },
  }
}
</script>

<style scoped>

</style>
